package com.quectel.emp.controller.device;


import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.core.module.device.dto.DeviceCategoryDto;
import com.quectel.core.module.device.dto.DeviceTypeDto;
import com.quectel.core.module.device.service.DeviceCategoryService;
import com.quectel.core.module.device.service.DeviceTypeService;
import com.quectel.emp.controller.BaseController;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-05-31 12:10:19
 */
@RestController
@RequestMapping("deviceType")
@Tag(name = "DeviceTypeController")
public class DeviceTypeController extends BaseController {

    @DubboReference
    private DeviceTypeService deviceTypeService;
    @DubboReference
    private DeviceCategoryService deviceCategoryService;

    /**
     * 查询所有种类对应类型
     */
    @GetMapping("queryAllByCode")
    @Operation(summary = "查询所有种类对应类型")
    public Response<List<DeviceTypeDto>> queryAll(@RequestParam String code) {
        DeviceCategoryDto deviceCategoryDto = deviceCategoryService.selectCacheByCode(code);
        Map<String, Object> params = new HashMap<>();
        if (deviceCategoryDto == null) {
            return Response.<List<DeviceTypeDto>>ok().wrap(new ArrayList<>());
        }
        params.put(LambdaUtil.getFieldName(DeviceTypeDto::getDeviceCategoryId), String.valueOf(deviceCategoryDto.getId()));
        List<DeviceTypeDto> list = deviceTypeService.queryList(params);
        return Response.<List<DeviceTypeDto>>ok().wrap(list);
    }
}
